<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title></title>
</head>

<body>
    <script type="text/javascript">
        // 	nums = [1,3], k = 3;
        let nums = [3, 2, 1, 5, 4],
            k = 2;
        var countKDifference = function(nums, k) {
            let res = 0,
                n = nums.length;
            const cnt = new Map();
            for (let j = 0; j < n; ++j) {

                res = res + (cnt.get(nums[j] - k) || 0) + (cnt.get(nums[j] + k) || 0);
                cnt.set(nums[j], (cnt.get(nums[j]) || 0) + 1);
            }
            return res;
        };

        let n = countKDifference(nums, k);
        document.write(n);
    </script>
</body>

</html>